<?php
namespace  Admin\Controller;
use Think\Controller;
class blogController extends Controller{
    public function add(){
        $this->display();
    }
    public function doadd(){
        $data['title']=I('post.title');
        $data['image']=I('post.image');
        $data['body']=I('post.body');
        $data['create_time']=  time();
        $data['uid']=session('id');    
        if($_FILES['image']['error']==0){
            move_uploaded_file($_FILES['image']['tmp_name'], THINK_PATH ."/../Public/upload/".md5(time()).".png") ; 
            $data['image']= 'THINK_PATH '.'/../Public/upload/'.md5(time()).'.png';
        }else{
            echo "文件上传错误";
        }     
        $Newsmodel=M(blog);
        $Newsmodel->data($data)->add();
        $this->redirect('Admin/blog/lists');
           }
    public function update(){
        $map['id']=I('get.id');
        $Newsmodel=M(blog);
        $data=$Newsmodel->where($map)->find();          
        $this->assign('data',$data);
        $this->display();
        }   
    public function doupdate(){   
        if($_FILES['images']['error']==0){
            move_uploaded_file($_FILES['images']['tmp_name'], 'THINK_PATH' ."/../Public/upload/".md5(time()).".png") ; 
            $data['image']='THINK_PATH' ."/../Public/upload/".md5(time()).".png" ;
        }
        else{
        $data['image']=I('post.image');
        }
        $data['title']=I('post.title');
        $data['body']=I('post.body');
        $data['update_time']=  time();       
        $map['id']=I('post.id');
        $Newsmodel=M(blog);
        $Newsmodel->where($map)->save($data);
        $this->redirect('Admin/blog/lists');
        }   
    public function delete(){
        $Newsmodel=M(blog);
        $map['id']=I('get.id');
        $Newsmodel->where($map)->delete();
        $this->redirect('Admin/blog/lists');
        }
    public function lists(){
      $Newsmodel=M('blog');
        $data=$Newsmodel
//                table用法 
//              ->table('__BLOG__  b,__USER__ u')
//              ->where('b.uid=u.id')
                ->field('b.*,u.name')
                ->table('__BLOG__ b')
                ->join(' __USER__ AS u ON  b.uid=u.id')
                ->select();                
        $this->assign('data',$data);
        $this->display();
    }  
}

